package com.java.admin.spareinventory.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.database.entity.Spareinventory;
import com.java.database.mapper.SpareinventoryMapper;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 库存表服务类层
 */
@Service
public class SpareinventoryService {

    @Autowired
    private SpareinventoryMapper spareinventoryMapper;

    /**
     * 添加
     */
    public void addSpareinventory(Spareinventory spareinventory){

        //判断添加对象是否为空
        if (spareinventory == null || "".equals(spareinventory)){
            throw new RuntimeException("添加失败请联系管理员:5623");
        }
        try{
            spareinventory.setSpareinventoryid(0);
            spareinventory.setStatus(0);
            //调用代理文件方法进行数据添加
            spareinventoryMapper.insert(spareinventory);

        }catch (Exception e){
            throw new RuntimeException("添加失败请联系管理员:5623");
        }
    }

    /**
     * 模糊分页查询
     * @param currentPage
     * @param theFuzzyValue
     * @return
     */
    public PageInfo selectAll(String currentPage,String theFuzzyValue){
        if (currentPage==null || "".equals(currentPage)){
            currentPage="1";
        }
        if (theFuzzyValue==null){
            theFuzzyValue="";
        }
        PageHelper.startPage(Integer.parseInt(currentPage),8);
        //查询数据
        List<Spareinventory> list = spareinventoryMapper.selectAll(theFuzzyValue);
        PageInfo pageInfo = new PageInfo(list);
        if (pageInfo==null || "".equals(pageInfo)){
            throw new RuntimeException("还没有数据请添加...");
        }
        return pageInfo;
    }

    /**
     * 下架配件
     * @param spareInventoryId
     */
    public void delecteById(String spareInventoryId){
        if (spareInventoryId==null || "".equals(spareInventoryId)){
            throw new RuntimeException("下架失败请联系管理员:5245");
        }
        try {
            spareinventoryMapper.updateById(Integer.parseInt(spareInventoryId));
        }catch (Exception e){
            throw new RuntimeException("下架失败请联系管理员:5245");
        }
    }

    /**
     * 通过id查询配件
     * @param spareInventoryId
     * @return
     */
    public Spareinventory selectById(String spareInventoryId){
        //判断是否存在id
        if (spareInventoryId==null || "".equals(spareInventoryId)){
            throw new RuntimeException("查询失败请联系管理员:4124");
        }
        Spareinventory spareinventory = spareinventoryMapper.selectByPrimaryKey(Integer.parseInt(spareInventoryId));
        return spareinventory;
    }

    /**
     * 修改库存信息
     * @param spareinventory
     */
    public void updateByPrimaryKey(Spareinventory spareinventory){
        if (spareinventory==null || "".equals(spareinventory)){
            throw new RuntimeException("修改失败请联系管理员:4512");
        }
        try{
            spareinventory.setStatus(0);
            spareinventoryMapper.updateByPrimaryKey(spareinventory);
        }catch (Exception e){
            throw new RuntimeException("修改失败请联系管理员:6524");
        }
    }

    /**
     * 查询下架配件
     * @param currentPage
     * @param theFuzzyValue
     * @return
     */
    public PageInfo selectOutOfStock(String currentPage,String theFuzzyValue){
        if (currentPage==null || "".equals(currentPage)){
            currentPage="1";
        }
        if (theFuzzyValue==null){
            theFuzzyValue="";
        }
        PageHelper.startPage(Integer.parseInt(currentPage),8);
        //查询数据
        List<Spareinventory> list = spareinventoryMapper.selectOutOfStock(theFuzzyValue);
        PageInfo pageInfo = new PageInfo(list);
        if (pageInfo==null || "".equals(pageInfo)){
            throw new RuntimeException("还没有数据请添加...");
        }
        return pageInfo;
    }

    /**
     * 通过id删除配件
     * @param spareinventoryId
     */
    public void deleteByPrimaryKey(String spareinventoryId){
        if (spareinventoryId==null || "".equals(spareinventoryId)){
            throw new RuntimeException("删除失败请联系管理员:5212");
        }
        try{
            spareinventoryMapper.deleteByPrimaryKey(Integer.parseInt(spareinventoryId));
        }catch (Exception e){
            throw new RuntimeException("删除失败请联系管理员:5212");
        }
    }

    /**
     * 配件上架
     */
    public void upateAdded(String spareInventoryId){
        if (spareInventoryId==null || "".equals(spareInventoryId)){
            throw new RuntimeException("上架失败请联系管理员:5452");
        }
        try{
            spareinventoryMapper.upateAdded(Integer.parseInt(spareInventoryId));
        }catch (Exception e){
            throw new RuntimeException("上架失败请联系管理员:5452");
        }
    }
}
